class Image < ActiveRecord::Base
  #Relations
  belongs_to :photographable, :polymorphic => true
  has_many   :edits, :as => :editable
  has_attached_file :image, :styles => { :default => "200x200#",
                                         :small => "50x50#",
                                         :medium => "100x100#" }

  #Named Scopes
  named_scope :logos, :conditions => { :image_type => 'Logo' }
  named_scope :photos, :conditions => { :image_type => 'Photo' }

  #Callbacks
  after_create :new_entry_edit
  after_update :create_edit

  #Validations
  validates_attachment_presence :image
  validates_attachment_size :image, :less_than => 5.megabytes
  validates_attachment_content_type :image, 
                                    :content_type => ['image/jpeg', 'image/png']

  private
  
  def to_s
    image_file_name
  end

  #
  #
  def new_entry_edit
    entry = Hash.new
    entry.store("added image", [image_file_name])
    Edit.create_edit(entry, photographable)
  end

  #
  #
  def create_edit
    if self.changed?
      Edit.create_edit(self.changes, photographable)
    end
  end


end
